package com.zhijian.medical.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import com.zhijian.medical.dao.local.CnasNewCorrectiveExcelMapper;
import com.zhijian.medical.entity.po.CnasNewCorrectiveExcel;
import com.zhijian.medical.entity.vo.request.CnasNewCorrectiveExcelSearchVo;
import com.zhijian.medical.entity.vo.request.CnasNewCorrectiveExcelSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.CnasNewCorrectiveExcelVo;
import com.zhijian.medical.service.CnasNewCorrectiveExcelService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * cnas不符合项表格业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2025-06-19 09:33:18
 */
@Slf4j
@Service
public class CnasNewCorrectiveExcelServiceImpl implements CnasNewCorrectiveExcelService {

	@Autowired
    private CnasNewCorrectiveExcelMapper mapper;

    public static final String CODE_PREFIX = "BLK-PF-009-01";

    @Override
    public CnasNewCorrectiveExcel selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public CnasNewCorrectiveExcelVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(CnasNewCorrectiveExcel cnasNewCorrectiveExcel) {
        Date now = new Date();
        cnasNewCorrectiveExcel.setCreateTime(now);
        cnasNewCorrectiveExcel.setUpdateTime(now);
        return mapper.insert(cnasNewCorrectiveExcel) > 0 ? cnasNewCorrectiveExcel.getId() : 0L;
    }

    @Override
    public Long insertSelective(CnasNewCorrectiveExcel cnasNewCorrectiveExcel) {
        Date now = new Date();
        cnasNewCorrectiveExcel.setCreateTime(now);
        cnasNewCorrectiveExcel.setUpdateTime(now);
        return mapper.insertSelective(cnasNewCorrectiveExcel) > 0 ? cnasNewCorrectiveExcel.getId() : 0L;
    }

    @Override
    public boolean update(CnasNewCorrectiveExcel cnasNewCorrectiveExcel) {
        Date now = new Date();
        cnasNewCorrectiveExcel.setUpdateTime(now);
        return mapper.updateByPrimaryKey(cnasNewCorrectiveExcel) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(CnasNewCorrectiveExcel record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(CnasNewCorrectiveExcelSaveOrUpdateVo saveOrUpdateVo) {
        CnasNewCorrectiveExcel cnasNewCorrectiveExcel = new CnasNewCorrectiveExcel();
        BeanUtils.copyProperties(saveOrUpdateVo, cnasNewCorrectiveExcel);
        // 记录编号
        String nextCode = createNextCode();
        cnasNewCorrectiveExcel.setRecordNum(nextCode);
        return this.insert(cnasNewCorrectiveExcel) != 0;
    }

    @Override
    public boolean modify(CnasNewCorrectiveExcelSaveOrUpdateVo saveOrUpdateVo) {
        CnasNewCorrectiveExcel old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        CnasNewCorrectiveExcel cnasNewCorrectiveExcel = new CnasNewCorrectiveExcel();
        BeanUtils.copyProperties(saveOrUpdateVo, cnasNewCorrectiveExcel);
        return this.updateByPrimaryKeySelective(cnasNewCorrectiveExcel);
    }

    @Override
    public CnasNewCorrectiveExcelVo selectOne(CnasNewCorrectiveExcelSearchVo cnasNewCorrectiveExcelSearchVo) {
        return mapper.selectOne(cnasNewCorrectiveExcelSearchVo);
     }

    @Override
    public Integer count(CnasNewCorrectiveExcelSearchVo cnasNewCorrectiveExcelSearchVo) {
        return mapper.count(cnasNewCorrectiveExcelSearchVo);
    }

    @Override
    public List<CnasNewCorrectiveExcelVo> selectList(CnasNewCorrectiveExcelSearchVo cnasNewCorrectiveExcelSearchVo) {
        return mapper.selectList(cnasNewCorrectiveExcelSearchVo);
    }

    @Override
    public Page<CnasNewCorrectiveExcelVo> selectPage(CnasNewCorrectiveExcelSearchVo cnasNewCorrectiveExcelSearchVo) {
        AssertUtil.check(Objects.isNull(cnasNewCorrectiveExcelSearchVo.getPageNum()) || Objects.isNull(cnasNewCorrectiveExcelSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(cnasNewCorrectiveExcelSearchVo);
        //分页
        Page<CnasNewCorrectiveExcelVo> page = new Page<>(true, count, cnasNewCorrectiveExcelSearchVo.getPageNum(), cnasNewCorrectiveExcelSearchVo.getPageSize());
        cnasNewCorrectiveExcelSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<CnasNewCorrectiveExcelVo> responseVos = this.selectList(cnasNewCorrectiveExcelSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<CnasNewCorrectiveExcel> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(CnasNewCorrectiveExcelSearchVo cnasNewCorrectiveExcelSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(cnasNewCorrectiveExcelSearchVo);
    }

    private String createNextCode() {
        String maxCode = mapper.selectMaxCodeByDay(DateUtil.formatDate(new Date()));
        if (StringUtils.isBlank(maxCode)) {
            return String.format("%s-001", CODE_PREFIX);
        }
        String[] maxCodeArray = maxCode.split("-");
        Integer i = Integer.valueOf(maxCodeArray[maxCodeArray.length - 1]);
        i++;
        // 如果是一位数，前面补两位0,如果是两位数，前面补一位0，如果是三位数，前面不补0
        if (i < 10) {
            return String.format("%s-00%s", CODE_PREFIX, i);
        } else if (i < 100) {
            return String.format("%s-0%s", CODE_PREFIX, i);
        }
        return String.format("%s-%s", CODE_PREFIX, i);
    }
}